﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using LibEBML.Common;
using VBFOOL.MediaPlayer.Core.Interface;
namespace VBFOOL.MediaPlayer.MKV
{
    class MKVMediaTrack : IMediaTrack
    {
        internal MKVMediaTrack(MKVFileSource source, TrackEntry track, MediaStreamType MediaType, ICodecFilter m_Codec)
        {
            m_source = source;
            Codec = m_Codec;
            Dictionary<string, object> obj = new Dictionary<string, object>();
            obj["CodecPrivateData"] = track.CodecPrivate;
            switch (track.TrackType)
            {
                case 1:
                    if (track.Video != null)
                    {
                        obj["Width"] = track.Video.PixelWidth;
                        obj["Height"] = track.Video.PixelHeight;
                    }
                    break;
                case 2:
                    if (track.Audio != null)
                    {
                        obj["Channels"] = track.Audio.Channels;
                        obj["SamplingFrequency"] = track.Audio.SamplingFrequency;
                        obj["OutSamplingFrequency"] = track.Audio.OutputSamplingFrequency;
                        obj["BitDepth"] = track.Audio.BitDepth;
                    }
                    break;
                default:
                    break;
            }
            var att = m_Codec.GetCodecPrivateData(obj);
            desc = new MediaStreamDescription(MediaType, att);
            this.Track = track;
            if (track.ContentEncodings != null && track.ContentEncodings.Count > 0)
            {
                var ce = track.ContentEncodings[0];
                if (ce.ContentEncodingType == 0)
                {
                    if (ce.Compression.ContentCompAlgo == 3)
                    {
                        CompressHeader = ce.Compression.ContentCompSettings;
                    }
                }
            }
            TimeCodeScale = m_source.Document.Segment.Info.TimecodeScale / 100;
            DefaultDuration = track.DefaultDuration / 100;
        }


        internal long TimeCodeScale = 10000;
        internal byte[] CompressHeader = null;
        internal long DefaultDuration = 0;
        internal TrackEntry Track = null;
        MediaStreamDescription desc;
        public MediaStreamDescription Description
        {
            get { return desc; }
        }

        public ICodecFilter Codec
        {
            get;
            protected set;
        }

        MKVFileSource m_source = null;
        public IInputSource Source
        {
            get { return m_source; }
        }
        public override string ToString()
        {
            if (Track != null && !string.IsNullOrEmpty(Track.Name))
            {
                return Track.Name;
            }
            else
            {
                return "(默认)";
            }
        }
    }
}
